package cc.vv.btpartyjob.partymanager.activity

import android.os.Message
import android.text.TextUtils
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.ProjectPostponeApplyActivityDelegate
import cc.vv.btpartyjob.partymanager.inter.YQApplyTimeClickInterface
import cc.vv.btpartyjob.partymanager.moudel.bean.ProjectDetailObj
import cc.vv.btpartyjob.partymanager.moudel.bean.ProjectYQObj
import cc.vv.btpartyjob.partymanager.moudel.req.ProjectYQRequestObj
import cc.vv.btpartyjob.partymanager.moudel.resp.ProjectYQResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 项目延期申请
 * Created by Chen.qi on 2018/10/30.
 */
class ProjectPostponeApplyActivity : BaseActivityMVP<ProjectPostponeApplyActivityDelegate>(), YQApplyTimeClickInterface {
    /**
     * 项目详情id
     */
    private var projectId: String? = null
    private var dataObj: ProjectDetailObj? = null

    override fun initData() {
        viewDelegate?.setTopBarTitle("延期申请")
        projectId = intent.getStringExtra("projectDetailId")
        dataObj = intent.getSerializableExtra("jieDuanList") as ProjectDetailObj?
        viewDelegate?.setProjectProessJieDuan(dataObj?.segmentationList, this)

        val content = dataObj?.projectIntroduction
        val yjTime = dataObj?.warningTime
        val completeTime = dataObj?.completeTime
        val state = dataObj?.state
        viewDelegate?.setZhengTiData(content, yjTime, completeTime, state)

    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })
        viewDelegate?.setOnClickListener(View.OnClickListener {
            //预警时间
            ResourceChooseServer.chooseData(this, handler, 5, 3)

        }, R.id.ll_ppaA_addStartTimeOne)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //预警完成时间
            ResourceChooseServer.chooseData(this, handler, 6, 3)

        }, R.id.ll_ppaA_addStartTimeTwo)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            val yqReason = viewDelegate?.getYQReason()
            if (TextUtils.isEmpty(yqReason)) {
                LKToastUtil.showToastShort("请输入延期原因")
                return@OnClickListener
            }

            val param = ArrayList<ProjectYQObj>()

            //阶段
            val segmentationList = dataObj?.segmentationList
            if (null != segmentationList) {
                for (i in 0 until segmentationList.size) {
                    val jieduanObj = segmentationList[i]
                    val proObj = ProjectYQObj()
                    val addYJTime = jieduanObj.addYJTime
                    val completeTime = jieduanObj.addWanChengTime
                    if (addYJTime > 1 && completeTime > 1) {
                        proObj.warningTime = jieduanObj.addYJTime
                        proObj.completeTime = jieduanObj.addWanChengTime
                        proObj.proId = projectId
                        proObj.reason = yqReason
                        proObj.segId = jieduanObj.id
                        proObj.type = 1
                        param.add(proObj)
                    }
                }
            }

            if (null != dataObj) {
                if (dataObj?.addZhengTiWanChengTime!! > 1 && dataObj?.addZhengTiYJtime!! > 1) {
                    val proObj = ProjectYQObj()
                    proObj.warningTime = dataObj?.addZhengTiYJtime
                    proObj.completeTime = dataObj?.addZhengTiWanChengTime
                    proObj.proId = projectId
                    proObj.reason = yqReason
                    proObj.segId = ""
                    proObj.type = 0
                    param.add(proObj)
                }
            }
            val request = ProjectYQRequestObj()
            request.param = param
            PartyManagerServer.projectApplyYq(request)

        }, R.id.tv_ppaA_applySubmit)
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is ProjectYQResponseObj -> {
                LKToastUtil.showToastShort("申请成功，等待审核")
                setResult(101)
                finish()
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        LKToastUtil.showToastShort(obj?.statusMessage)
    }

    override fun getDelegateClass(): Class<ProjectPostponeApplyActivityDelegate> {
        return ProjectPostponeApplyActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun changeYJtime(pos: Int, yJTime: String?) {
        dataObj?.segmentationList?.get(pos)?.addYJTime = LKTimeUtil.getInstance().getLongTime(yJTime, "yyyy-MM-dd")
    }

    override fun changeCompleteTime(pos: Int, completeTime: String?) {
        dataObj?.segmentationList?.get(pos)?.addWanChengTime = LKTimeUtil.getInstance().getLongTime(completeTime, "yyyy-MM-dd")
    }

    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            5 -> {
                //整体预警时间
                val wayString: String = msg.obj as String
                val acceptTime = LKTimeUtil.getInstance().getLongTime(wayString, "yyyy-MM-dd")
                dataObj?.addZhengTiYJtime = acceptTime
                viewDelegate?.setZhentTiYJTime(wayString)
            }

            6 -> {
                //预计完成时间
                val dateString: String = msg.obj as String
                val acceptTime = LKTimeUtil.getInstance().getLongTime(dateString, "yyyy-MM-dd")
                dataObj?.addZhengTiWanChengTime = acceptTime
                viewDelegate?.setZhentTiCompleteTime(dateString)
            }
        }
    }

}